package com.mc2.docapp.disabilityassessmenttool;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.mc2.docapp.disabilityassessmenttool.utils.MailerObject;
import com.mc2.docapp.disabilityassessmenttool.utils.SendMailTLS;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

    public static void saveExcelFile(FileOutputStream outputStream) throws IOException {

        Workbook wb = new HSSFWorkbook();

        CellStyle cs = wb.createCellStyle();
        cs.setFillForegroundColor(HSSFColor.LIME.index);

        //
        SampleExcelDTO sampleExcelDTO = new SampleExcelDTO(1,1,"c","d",new Date());
        mapDtoToExcel(SampleExcelDTO.class);
        //


        Sheet sheet1 = wb.createSheet("wrok");

        Row row = sheet1.createRow(0);
        Cell c = row.createCell(0);
        c.setCellValue("Name");
        c.setCellStyle(cs);
        c = row.createCell(1);
        c = row.createCell(2);
        c.setCellValue("M");
        c.setCellStyle(cs);
        c = row.createCell(3);
        c.setCellValue("A");
        c.setCellStyle(cs);
        c = row.createCell(4);
        c.setCellValue("E");
        c.setCellStyle(cs);
        int rowOne = 1;

        final Row row1 = sheet1.createRow(rowOne);
        Cell cell1 = row1.createCell(0);
        cell1.setCellValue("Name");
        cell1 = row1.createCell(1);
        cell1.setCellValue("Morning");
        cell1 = row1.createCell(2);
        cell1.setCellValue("Afternoon");
        cell1 = row1.createCell(3);
        cell1.setCellValue("Evening");

        sheet1.setColumnWidth(2, (15 * 500));
        wb.write(outputStream);
    }

    private static void mapDtoToExcel(Class<SampleExcelDTO> sampleExcelDTOClass) {
        System.out.println("sampleExcelDTOClass.getMethods() = " + sampleExcelDTOClass.getMethods());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        {
            final int PERMISSION_REQUEST_CODE = 1;

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {

                if (checkSelfPermission(Manifest.permission.SEND_SMS)
                        == PackageManager.PERMISSION_DENIED) {

                    // Log.d("permission", "permission denied to SEND_SMS - requesting it");
                    String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.ACCESS_NETWORK_STATE,Manifest.permission.INTERNET};
                    //ACCESS_NETWORK_STATE
                    requestPermissions(permissions, PERMISSION_REQUEST_CODE);

                }
            }

        }

        Button aboutButton = (Button) findViewById(R.id.about);
        aboutButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               /* Intent intent = new Intent(MainActivity.this, AmazonS3Activity.class);
                startActivity(intent);*/
                Intent intent = new Intent(MainActivity.this, About.class);
                startActivity(intent);
            }
        });

        Button newPatientButton = (Button) findViewById(R.id.newPatient);
        newPatientButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, Instructions.class);
                startActivity(intent);
            }
        });

        Button archievedRecordButton = (Button) findViewById(R.id.archivedRecord);
        archievedRecordButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //
                try {
//                    getPublicAlbumStorageDir("saurabh.xls");
                    FileOutputStream fos = new FileOutputStream(new File("/storage/emulated/0/Pictures/saurabh.xls"));
                    //saveExcelFile(openFileOutput("saurabh.xlsx", Context.MODE_PRIVATE));
                    saveExcelFile(fos);
                    fos.close();
                    {
                        try {
                            String FILENAME = "/storage/emulated/0/Pictures/saurabh.xls";
                            SendMailTLS.getMailer().sendMail(new MailerObject("swapnildeshpande67@gmail.com", "MC2 Docapp", FILENAME), true);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //
                Intent intent = new Intent(MainActivity.this, About.class);
                startActivity(intent);
            }
        });

        Button exitButtton = (Button) findViewById(R.id.exit);
        exitButtton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    Intent shareIntent = new Intent(Intent.ACTION_SEND);
                    shareIntent.setType("text/plain");
                    shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Disability Assessment App");
                    String shareMessage= "\nHey There, Let me recommend you this Disability Assessment Application\n\n";
                    shareMessage = shareMessage + "https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID +"\n\n";
                    shareIntent.putExtra(Intent.EXTRA_TEXT, shareMessage);
                    startActivity(Intent.createChooser(shareIntent, "choose one"));
                } catch(Exception e) {
                    System.out.println(e.getMessage());
                }
            }
        });
    }
}
